#include "downloadthreadtask.h"
#include <qthread.h>
#include <qdebug.h>

DownloadThreadTask::DownloadThreadTask(const QString& saveFileName, const QString& mp3URL,QObject *parent)
	: QObject(parent)
	, m_fileName(saveFileName)
	, m_downloadURL(mp3URL)
{}

DownloadThreadTask::~DownloadThreadTask()
{}

void DownloadThreadTask::onHandleTask()
{
	qDebug() << "onHandleTask";
	m_manager = new KHttpManager(this);
	connect(m_manager, &KHttpManager::readyDownloadHttp, this, &DownloadThreadTask::taskFinished);
	connect(m_manager, &KHttpManager::downloadProgress, this, &DownloadThreadTask::onDownloadProgress);
	m_manager->httpDownload(m_downloadURL, m_fileName);
}

void DownloadThreadTask::onDownloadProgress(qint64 byteRecieve, qint64 byteTotal)
{
	int progress = 0;
	if(byteTotal > 0)
		progress = byteRecieve * 100 / byteTotal;
	emit progressUpdated(progress);
}
